package cn.slipi.admin.task;

import cn.slipi.admin.biz.vo.consumer.GrantPublishVo;
import cn.slipi.admin.biz.vo.iface.ApiPublishVo;
import cn.slipi.admin.common.contants.ZkPathConstants;
import cn.slipi.admin.common.utils.DateUtil;
import cn.slipi.admin.common.utils.dozer.impl.ConvertBean;
import cn.slipi.admin.entity.CsmGrant;
import cn.slipi.admin.entity.PdrApi;
import cn.slipi.admin.service.CsmGrantService;
import cn.slipi.admin.service.PdrApiService;
import com.alibaba.fastjson.JSONObject;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PublishTask {

    @Autowired
    private ZkClient zkClient;

    @Autowired
    private ConvertBean convertBean;

    @Autowired
    private PdrApiService pdrApiService;

    @Autowired
    private CsmGrantService csmGrantService;

    public void unPublishCsmGrant(CsmGrant csmGrant) {
        if (csmGrant.getPublishTime() <= 0) {
            return;
        }
        String grantZkPath = ZkPathConstants.buildGrantZkPath(csmGrant.getPdrApiCode());
        if (!zkClient.exists(grantZkPath)) {
            return;
        }
        String bottomGrantZkPath = ZkPathConstants.buildBottomGrantZkPathWithoutEncode(csmGrant.getPdrApiCode(), csmGrant.getAppKey());
        if (!zkClient.exists(bottomGrantZkPath)) {
            return;
        }
        zkClient.delete(bottomGrantZkPath);
    }

    @Async
    public void publishCsmGrant(List<CsmGrant> csmGrantList) {
        csmGrantList.forEach(csmGrant -> {
            String grantZkPath = ZkPathConstants.buildGrantZkPath(csmGrant.getPdrApiCode());
            if (!zkClient.exists(grantZkPath)) {
                zkClient.createPersistent(grantZkPath, true);
            }
            String bottomGrantZkPath = ZkPathConstants.buildBottomGrantZkPathWithoutEncode(csmGrant.getPdrApiCode(), csmGrant.getAppKey());
            if (!zkClient.exists(bottomGrantZkPath)) {
                zkClient.createPersistent(bottomGrantZkPath, true);
            }
            GrantPublishVo publish = this.convertBean.convert(csmGrant, GrantPublishVo.class);
            String publishSnap = JSONObject.toJSONString(publish);
            zkClient.writeData(bottomGrantZkPath, publishSnap);
            //更新发布时间及快照
            csmGrant.setPublishTime(DateUtil.getTimeByMinSecond());
            csmGrant.setPublishSnap(publishSnap);
            this.csmGrantService.getBaseMapper().updateById(csmGrant);
        });
    }

    @Async
    public void publishApiBatch(List<Integer> apiIds) {
        if (apiIds == null || apiIds.size() == 0) {
            return;
        }
        List<PdrApi> pdrApiList = this.pdrApiService.getListByIds(apiIds);
        pdrApiList.forEach(pdrApi -> {
            ApiPublishVo publish = this.convertBean.convert(pdrApi, ApiPublishVo.class);
            String apiZkPath = ZkPathConstants.buildApiZkPath(publish.getCode());
            if (!zkClient.exists(apiZkPath)) {
                zkClient.createPersistent(apiZkPath, true);
            }
            String publishSnap = JSONObject.toJSONString(publish);
            zkClient.writeData(apiZkPath, publishSnap);
            //更新发布时间及快照
            pdrApi.setPublishSnap(publishSnap);
            pdrApi.setPublishTime(DateUtil.getTimeByMinSecond());
            this.pdrApiService.getBaseMapper().updateById(pdrApi);
        });
    }
}
